package com.zlzxm.shoesmarket.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityManageInfo;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityChartValue;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityIn;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityInOut;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * CREATE BY zlzxm ON 4/23/21
 */
public class VMManageDesc extends ViewModel {


    private MutableLiveData<List<EntityManageInfo>> liveManage = new MutableLiveData<>();

    private MutableLiveData<EntityIn> liveIn = new MutableLiveData<>();

    public MutableLiveData<List<EntityManageInfo>> getLiveManage() {
        return liveManage;
    }

    public VMManageDesc() {

        liveManage.setValue(createInfo());
    }

    public MutableLiveData<EntityIn> getLiveIn() {
        return liveIn;
    }

    public void requestInfo(){

        Observable.timer(2, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveManage.setValue(createTestData());
                    }
                });



    }



    public void getInData(){

        Observable.timer(1, TimeUnit.SECONDS)
                .compose(RxAndroidHelp.turn())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Long aLong) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                        liveIn.setValue(createTestInData());
                    }
                });




    }


    //<editor-fold defaultState="collapsed" desc="私有方法">



    private EntityManageInfo createInfo(String key,boolean isUp, String rate
    ){


        return new EntityManageInfo(key,isUp,rate);

    }







    private List<EntityManageInfo> createInfo(){


        List<EntityManageInfo> list = new ArrayList<>();


        list.add(createInfo("今日销售：",true,"0%"));
        list.add(createInfo("今日出库数：",true,"0%"));
        list.add(createInfo("今日净利润：",true,"0%"));
        list.add(createInfo("今日入库数：",true,"0%"));

        return  list;

    }


    // </editor-fold>


    //<editor-fold defaultState="collapsed" desc="测试">


    private List<EntityManageInfo> createTestData(){


        List<EntityManageInfo> list = new ArrayList<>();


        list.add(createInfo("今日销售：878",true,"0%"));
        list.add(createInfo("今日出库数：878",true,"0%"));
        list.add(createInfo("今日净利润：878",true,"0%"));
        list.add(createInfo("今日入库数：878",true,"0%"));

        return  list;

    }


    private EntityIn createTestInData(){


        List<EntityChartValue> listIn = new ArrayList<>();

        List<EntityChartValue> listOut = new ArrayList<>();



        listIn.add(new EntityChartValue("周一",18));
        listIn.add(new EntityChartValue("周二",40));
        listIn.add(new EntityChartValue("周三",28));
        listIn.add(new EntityChartValue("周四",42));
        listIn.add(new EntityChartValue("周五",58));
        listIn.add(new EntityChartValue("周六",89));
        listIn.add(new EntityChartValue("周日",80));


        listOut.add(new EntityChartValue("周一",0));
        listOut.add(new EntityChartValue("周二",21));
        listOut.add(new EntityChartValue("周三",8));
        listOut.add(new EntityChartValue("周四",31));
        listOut.add(new EntityChartValue("周五",20));
        listOut.add(new EntityChartValue("周六",50));
        listOut.add(new EntityChartValue("周日",100));


        return  new EntityIn(listIn,listOut);

    }


    // </editor-fold>
}
